/**
 * Created by ThinkPad on 2018/10/25.
 */
//审核状态字典翻译
function shztFormate(value, options, row){
    //sex为自定义字典项
    var statusObj = vm.shztObject.shzt;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
//是否学完课程字典翻译
function sfxwFormate(value, options, row){
    //sex为自定义字典项
    var statusObj = vm.sfxwObject.sfxw;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
    return '<span></span>';
}

function openShModal(xy_id,userid) {
    $('#myModal').modal({
        keyboard: true
    });
    vm.title = "审核";
    vm.xy_id = xy_id;
    vm.userid = userid;
}

function showImages(xy_id,bmlx) {
    //先关闭菜单实现刷新效果
    parent.closeTabByPageId("10011");
    //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
    parent.addTabs({id:'10011',title: '查看图片',close: true,url: 'modules/onlinestudy/showImages.html?xy_id='+xy_id+'&bmlx='+bmlx,urlType: 'relative'});
}

function showImgarr(xy_id,bmlx){
    if(bmlx == '0') {
        //先关闭菜单实现刷新效果
        parent.closeTabByPageId("10012");
        //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
        parent.addTabs({
            id: '10012',
            title: '查看抽取图片',
            close: true,
            url: 'modules/onlinestudy/manfen.html?xy_id=' + xy_id + '&bmlx=' + bmlx,
            urlType: 'relative'
        });
    }else{
        //先关闭菜单实现刷新效果
        parent.closeTabByPageId("10003");
        //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
        parent.addTabs({id:'10003',title: '查看抽取图片',close: true,url: 'modules/onlinestudy/shenyan.html?xy_id='+xy_id+'&bmlx='+bmlx,urlType: 'relative'});
    }
}

function dataOption(cellvalue, options, rowObject){
    var showImgArr_string = "";
    if(rowObject.imgarr && rowObject.shzt == '1'){
        showImgArr_string = "<font color='blue'><a href='#' onclick='showImgarr("+rowObject.id+","+rowObject.bmlx+");'>打印</a></font>";
    }
    return showImgArr_string;
}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'onlinestudy/xcbm/list',
        datatype: "json",
        mtype:"post",
        postData:{"bmlx":bmlx},
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
            { label: '学员姓名', name: 'xyxm', index: 'xyxm', width: 80 },
            { label: '驾驶证号', name: 'jszh', index: 'jszh', width: 130 },
            { label: '档案编号', name: 'dabh', index: 'dabh', width: 100 },
            { label: '学习地点', name: 'address', index: 'address', width: 80 },
            { label: '是否学完', name: 'sfxw', index: 'sfxw', width: 60,formatter: sfxwFormate},
            { label: '审核状态', name: 'shzt', index: 'shzt', width: 60,formatter:shztFormate },
            { label: '报名时间', name: 'ldate', index: 'ldate', width: 120, formatter:formatSubscribeTime },
            { label: '报名类型', name: 'bmlx', index: 'bmlx', width: 80,hidden:true},
            { label: '用户id', name: 'userid', index: 'userid', width: 80,hidden:true },
            { label: '头像', name: 'avatar', index: 'avatar', width: 80 ,hidden:true},
            { label: '', name: 'imgarr', index: 'imgarr', width: 80,hidden:true },
            { label: '操作', name: '', index: '', width:160,formatter:dataOption}
        ],
        viewrecords: true,
        height: 290,
        rowNum: 8,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        xcBm: {"bmlx":bmlx},
        sfxwObject: {},
        shztObject: {},
        shcz:'',
        shczObject:{},
        xy_id:'',
        studyAddresss:[]
    },
    mounted:function(){
        this.$nextTick(function(){ this.initSysDict()});
        this.dateDefault();
    },
    methods: {
        initSysDict:function(){
            $.get(baseURL+"sys/dict/select/shzt",function(r){
                if(r.code === 0){
                    vm.shztObject = r.dict;
                    vm.xcBm.shzt="";
                    var shzt_array = [];
                    r.dict.shzt.forEach((e) => {
                        if(e.value != 0){
                            shzt_array.push(e);
                        }
                    });
                    vm.shczObject = shzt_array;
                    vm.shcz = '1';
                }else{
                    console.log(r.msg);
                }
            });

            $.get(baseURL+"sys/dict/select/sfxw",function(r){
                if(r.code === 0){
                    vm.sfxwObject = r.dict;
                    vm.xcBm.sfxw="";
                }else{
                    console.log(r.msg);
                }
            });
            $.get(baseURL+"onlinestudy/xcbm/xxAddressList",function(r){
                if(r.code === 0){
                    vm.studyAddresss = r.studyAddresss;
                    vm.xcBm.address="";
                }else{
                    console.log(r.msg);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            var paras = {
                bmlx:vm.xcBm.bmlx,
                xyxm:vm.xcBm.xyxm,
                jszh:vm.xcBm.jszh,
                sfxw:vm.xcBm.sfxw ==""?undefined:vm.xcBm.sfxw,
                shzt:vm.xcBm.shzt ==""?undefined:vm.xcBm.shzt,
                ldate_start:$("#ldate_start").val() == ""?undefined:$("#ldate_start").val(),
                ldate_end:$("#ldate_end").val() == ""?undefined:$("#ldate_end").val(),
                address:vm.xcBm.address == ""?undefined:vm.xcBm.address
            };
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:paras
            }).trigger("reloadGrid");
        },
        dateDefault:function(){
            //初始化日期范围组件
            $('#datepicker_range input').datepicker({
                language: 'zh-CN',//显示中文
                startView: 0, //选择器开始界面 0：天（默认 1：月2：年3：十年 4：世纪
                minViewMode: 0,//最小选择精度 0：天（最小默认）
                maxViewMode: 2, //最大选择精度 4：世纪（最大默认）
                format: 'yyyy-mm-dd',//显示格式
                minView: "day",//设置只显示到月份
                // initialDate: new Date(),
                autoclose: true,//选中自动关闭
                // todayBtn: true//显示今日按钮
                clearBtn: true, //清除按钮
                todayHighlight: true //今日高亮
            });
        },
        executeSh: function(){
            confirm("确认执行审核操作？",function(){
                $("#myModal").modal('hide');
                var url = "onlinestudy/xcbm/executeSh";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify({"id":vm.xy_id,"shzt":vm.shcz,"userid":vm.userid}),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            })
        }
    }
});